<?php

namespace app\helpers;

use app\constant\CommonConstant;
use think\helper\Arr;
use think\Request;

class FormatHelper
{
    public static function getLoadingUrl(string $website, string $checkout_id, array $params = []): string
    {
        $host = domain($website);
        $url = sprintf("%s" . CommonConstant::ORDER_LOAGING_PATH, $host, $checkout_id);
        if ($params) {
            $url = $url . '?' . http_build_query($params);
        }
        return $url;
    }

    public static function getCampaignParams(Request $request): array
    {
        $referer = $request->header('referer', '');
        $urlParams['website'] = $referer;
        $urlParams['user_agent'] = $request->header('User-Agent');
        $campaign_id = $request->param('campaign_id', 0);
        if (!$campaign_id) {
            $utm_id = $request->param('utm_id', 0);
            if ($utm_id) $campaign_id = $utm_id;
        }
        $urlParams['campaign_id'] = $campaign_id;
        $urlParams['traffic_platform'] = CommonConstant::TRAFFIC_SOURCE_FB;
        $fbclid = $request->param('fbclid', '');
        if (!$fbclid) {
            $ttclid = $request->param('ttclid', '');
            if ($ttclid) {
                $urlParams['fbclid'] = $ttclid;
                $urlParams['traffic_platform'] = CommonConstant::TRAFFIC_SOURCE_TK;
            }
        }
        $urlParams["fbc_at"] = round(microtime(true) * 1000);
        $urlParams['ad_id'] = $request->param('ad_id', 0);
        $urlParams['adset_id'] = $request->param('adset_id', 0);
        return $urlParams;
    }

}